package com.my.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my.demo.pojo.EnterpriseSpace;
import com.my.demo.pojo.EnterpriseUser;
import com.my.demo.pojo.Permission;
import com.my.demo.pojo.Result;
import com.my.demo.service.EnterpriseSpaceService;
import com.my.demo.service.EnterpriseUserService;
import com.my.demo.service.PermissonService;
import com.my.demo.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class EnterpriseSpaceController {
    @Autowired
    EnterpriseSpaceService enterpriseSpaceService;
    @Autowired
    EnterpriseUserService enterpriseUserService;
    @Autowired
    UserService userService;
    @Autowired
    PermissonService permissonService;



    @PostMapping("/get_enterprise_space")
    @CrossOrigin
    public Result getEnterpriseSpace(){
        Result result = new Result<>();
        result.setSuccess(false);
        result.setMsg(null);
        //获取当前用户的用户id，并且获取该用户所在的所有企业空间
        String userName = (String) SecurityUtils.getSubject().getPrincipal();
        Integer userId = userService.selectIdByName(userName);
        //获取其在的所有企业空间编号
        QueryWrapper<EnterpriseUser> enterpriseUserQueryWrapper = new QueryWrapper<>();
        if (userId!=null&&userId>0){
            enterpriseUserQueryWrapper.eq("user_no",userId);
        }
        else {
            return result;
        }
        List<EnterpriseUser> enterpriseUserList = null;
        try{
            enterpriseUserList = enterpriseUserService.list(enterpriseUserQueryWrapper);
        }catch (Exception exception){
            exception.printStackTrace();
        }
        //如果列表为空，说明该用户没有企业空间
        if (enterpriseUserList==null||enterpriseUserList.isEmpty()){
            return result;
        }
        //获取该用户所有的企业空间的编号
        List<Integer> spaceNoList = new ArrayList<>();
        for (EnterpriseUser enterpriseUser : enterpriseUserList) {
            spaceNoList.add(enterpriseUser.getSpaceNo());
        }

        //接下来找到该企业空间编号对应的企业空间的信息
        QueryWrapper<EnterpriseSpace> enterpriseSpaceQueryWrapper = new QueryWrapper<>();
        enterpriseSpaceQueryWrapper.in("space_no",spaceNoList);
        List<EnterpriseSpace> enterpriseSpaceList = null;
        try {
            enterpriseSpaceList=enterpriseSpaceService.list(enterpriseSpaceQueryWrapper);
        }catch (Exception exception){
            exception.printStackTrace();
        }
        if (enterpriseSpaceList==null||enterpriseSpaceList.isEmpty()){
            return result;
        }

        result.setDetail(enterpriseSpaceList);
        result.setSuccess(true);
        result.setMsg("查询企业空间成功");
        return result;
    }
    @PostMapping("/change_enterprise_space")
    @CrossOrigin
    public Result changeEnterpriseSpace(@RequestBody Map<String,Integer> map){
        Result result = new Result();
        result.setMsg(null);
        result.setSuccess(false);

        //获取当前用户的用户id，并且获取该用户所在的所有企业空间
        String userName = (String) SecurityUtils.getSubject().getPrincipal();
        Integer userId = userService.selectIdByName(userName);


        Integer spaceId = 0;
        try {
            spaceId = map.get("space_no");
        }catch (Exception exception){
            exception.printStackTrace();
        }
        if (spaceId!=null&&spaceId>0){
            System.out.println("当前更换的企业空间的id为："+spaceId);
            result.setSuccess(true);
            result.setMsg("切换企业空间成功");
            permissonService.saveOrUpdate(new Permission(userId,spaceId));

        }
        return result;
    }

}
